package com.test.smartcampusapi.controller;

import com.test.smartcampusapi.entity.Card;
import com.test.smartcampusapi.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Date : 4/23/2022 12:09 AM
 * Author : lingy
 * Description : ...
 */
@RestController
public class CardController {
    @Autowired
    private CardService cardService;

    @PostMapping(value = "/card/create")
    public String createCard(@RequestBody Map<String, String> card) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String registryDate = dateFormat.format(date);
        String cardNumber = card.get("cardNumber").equals("") ? System.currentTimeMillis() + "" : card.get("cardNumber");
        try {
            cardService.createCard(cardNumber, registryDate);
            return cardNumber;
        } catch (Exception e) {
            return "-1"; //如果返回-1说明创建失败
        }
    }

    @PostMapping(value = "/card/updateStatus")
    public int updateStatus(@RequestBody Map<String, String> card) {
        String cardNumber = card.get("cardNumber");
        String status = card.get("status");
        cardService.updateCardStatus(cardNumber, status);
        return 0;
    }

    @PostMapping(value = "/card/getCredit")
    public String getCredit(@RequestBody Map<String, String> creditObject) {
        String cardNumber  = creditObject.get("cardNumber");
        return cardService.getCredit(cardNumber)+"";
    }

    @PostMapping(value = "/card/getAllCards")
    public List<Card> getAllCards(){
        return cardService.getAllCards();
    }


}
